<?php
/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not 
 *  exist, see <http://www.gnu.org/licenses/>.
 * 
 * $LastChangedDate: 2008-07-10 23:59:20 +0200 (Thu, 10 Jul 2008) $
 * $Rev: 1285 $
 * $Author: gavinspearhead $
 * $Id: login.php 1285 2008-07-10 21:59:20Z gavinspearhead $
 */

if (!defined('ORIGINAL_PAGE')) define('ORIGINAL_PAGE', $_SERVER['PHP_SELF']);

$pathlo = realpath(dirname(__FILE__));
// We can't include html_includes as that would hit the checkauth and forward to login.php -> endless loop

require_once "$pathlo/../functions/functions.php";
require_once "$pathlo/../functions/web_functions.php";


$process_name = 'urd_web'; // needed for message format in syslog and logging 
// Is URD actually installed?
if (!file_exists('../.installed')) {
        header('Location: ../install/install.php');
}
if (isset($config['urdweb_logfile']))
	$config['log_file'] = $config['urdweb_logfile'];
else 
	$config['log_file'] = '/dev/null';


require_once "$pathlo/../functions/urd_log.php";

try {
	$db = connect_db();  // initialise the database
} catch (exception $e) {
	echo "Connection to database failed.\n";
	die;
}

require_once "$pathlo/../functions/smarty.php" ;
$languages = get_languages();
$language = get_request('language_name', NULL);
if (!in_array($language, array_keys($languages)))
    $language = select_language($db, NULL);

if (isset($_POST['username']) && isset($_POST['pass']) && isset($_POST['period']) && $_POST['pass'] != '' && $_POST['username'] != ''  ) {
    require_once "$pathlo/../functions/checkauth.php";
    if (isset($valid) && $valid === TRUE) {
        $lang = get_pref($db, 'language', $userID);
        if ($lang == '')
            set_pref($db, 'language', $language, $userID);
        redirect('index.php');
    }
}

$prefs = load_config($db);

load_language($language);

$title = 'URD - ' . $LN['login_title'];

$register = ((isset ($prefs['register']) && $prefs['register'] > 0)) ? 1 : 0;

$ip_address = $_SERVER['REMOTE_ADDR'];
$token = generate_password(8);

if (isset($__message)) 
    $smarty->assign('__message', $__message);
$smarty->assign('title', $title);
$smarty->assign('ip_address', $ip_address);
$smarty->assign('register', $register);
$smarty->assign('token', $token);
$smarty->assign('languages', $languages);
$smarty->assign('curr_language', $language);

$smarty->assign('menu',	    generate_menu($LN));
$smarty->display('login.tpl');

?>
